package com.ftg.learn.chapter03;

import jdk.jfr.internal.Options;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class LearnStream {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>(4);
        list.add("张三丰");
        list.add("梅超峰");
        list.add("张无忌");
        list.add("张无忌");
        list.add("赵敏");
        list.add("周指惹");

        //jdk1.8 new future stream : 流操作
        //声明
        //操作
        //终止
        Stream<String> stream = list.stream();

        System.out.println(list.stream().filter(x -> x.startsWith("张")).count());

        System.out.println(list.stream().filter(x->x.startsWith("张")).collect(Collectors.toList()));

        System.out.println(list.stream().filter(x->x.startsWith("张")).collect(Collectors.joining("-")));
        System.out.println(list.stream().distinct().collect(Collectors.joining("-")));
        System.out.println(list.stream().distinct().filter(x->x.startsWith("张")).collect(Collectors.joining("-")));

        int[] a = {1,2,3,4,5};
        System.out.println(Stream.of(1, 2, 3, 4, 5, 6, 7, 8).filter(x -> x > 2).count());
        System.out.println(Arrays.stream(a).filter(x -> x > 2).count());

        String s = "avfdsafasfds";
        System.out.println(Arrays.toString(s.split("")));

        System.out.println(Arrays.stream(s.split("")).filter(x -> x.charAt(0) > 'x').collect(Collectors.joining("--")));

        System.out.println(Stream.of(s).count()+"===>");

        System.out.println(list.stream().skip(4).limit(2).collect(Collectors.toList()));

        System.out.println(list.stream().map(x->x.substring(0,1)).distinct().collect(Collectors.toList()));

        System.out.println(list.stream().map(x -> "姓名" + x).collect(Collectors.toList()));

        System.out.println(list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()));
        System.out.println(list.stream().sorted().collect(Collectors.toList()));

        Optional<String> op = list.stream().reduce((x,y)->{
            System.out.println(x);
            System.out.println(y);
            return y.substring(1) + "^_^"+","+x;
        });

        System.out.println(op.get());


        for (int i = 0; i < 10; i++) {
            String any = list.stream().findFirst().get();
            System.out.println(any);
            String any1 = list.stream().findAny().get();
            System.out.println(any1);
        }



        /**
         * 声明
         *  集合  .stream();
         *  Stream.of(大量参数)
         *  Arrays.stream(可以是一个数组)
         *  Arrays.stream(s.split("") 字符串转换成stream
         *  -----
         * 操作 : 中间操作
         * filter 过滤
         * distinct() 去重复
         * skip()跳越过多少个
         * limit() 显示几个
         * map（） 收集整理，返回你想要的数据
         *
         *
         * ---
         * 终止
         * count() 统计 long
         * collect() 用来收集数据
         *    Collectors.toList()  转换成list
         *    Collectors.joining(分隔符) 转换成字符串
         */

    }

}
